/*
 * MIT License
 *
 * Copyright (c) 2020 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.openapi.request;

import cn.seaboot.postman.request.Query;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Digits;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Parameter;

/**
 * hibernate-validation 参数校验扫描
 * <p>
 * postman 操作面板上空间有限，无法填写太多内容，一般使用这个默认的即可。
 * <p>
 * 就个人的编码习惯而言，我并不希望 hibernate-validation 处理很复杂的逻辑，
 * {@link Validated} 的分组基本没用，因为会导致校验规则非常混乱，
 * <p>
 * 对于 hibernate-validation 的使用，个人推荐：公用的部分，写在 ValueObject 中，独有的部分，写在 Parameter 上。
 *
 * @author Mr.css
 * @version 2023-09-18 16:20
 */
public class DefaultValidatorAnalyzer implements ValidatorAnalyzer {

    @Override
    public void resolveParameter(Parameter parameter, Query query) {
        this.resolve(parameter, query);
    }

    @Override
    public void resolveField(Parameter parameter, Field field, Query query) {
        this.resolve(field, query);
    }

    private void resolve(AnnotatedElement element, Query query) {
        StringBuilder sb = new StringBuilder();
        sb.append(query.getDescription());

        if (element.getAnnotation(NotEmpty.class) != null) {
            sb.append("|NotEmpty");
        }
        Size size = element.getAnnotation(Size.class);
        if (size != null) {
            sb.append("|Size(").append(size.min()).append(",").append(size.max()).append(")");
        }
        Length length = element.getAnnotation(Length.class);
        if (length != null) {
            sb.append("|Size(").append(length.min()).append(",").append(length.max()).append(")");
        }
        Digits digits = element.getAnnotation(Digits.class);
        if (digits != null) {
            sb.append("|Digits(").append(digits.integer()).append(",").append(digits.fraction()).append(")");
        }
        query.setDescription(sb.toString());
    }
}
